

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>新建项目</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="__PUBLIC__/style/admin.css" media="all">
</head>
<body>  
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-body">
            <div class="layui-form">
              <div class="layui-form-item">
                <label class="layui-form-label">项目名称</label>
                <div class="layui-input-block">
                  <input type="text" name="name" autocomplete="off" placeholder="请输入项目名称" lay-verify="required" class="layui-input">
                </div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label">项目描述</label>
                <div class="layui-input-block">
                  <textarea name="desc" placeholder="请输入项目描述" class="layui-textarea"></textarea>
                </div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label">短信通知</label>
                <div class="layui-input-block">
                  <select name="sms" lay-filter="sms">
                    <option value="0" selected>关闭</option>
                    <option value="1">开启</option>
                  </select>
                </div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label">模板选择</label>
                <div class="layui-input-block">
                  <volist name="templates" id="vo">
                    <input type="radio" name="template" value="{$vo.id}" title="{$vo.name}" data-tips="{$vo.desc}">
                  </volist>
                </div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label">载荷选择</label>
                <div class="layui-input-block">
                  <volist name="payloads" id="vo">
                    <input type="checkbox" name="payload[]" value="{$vo.id}" title="{$vo.name}" data-tips="{$vo.desc}">
                  </volist>
                </div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                  <button class="layui-btn" lay-submit lay-filter="add-submit">创建</button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="__PUBLIC__/layui/layui.js"></script>
  <script>
  layui.config({
    base: '__PUBLIC__/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form'], function(){
    var form = layui.form, $ = layui.$;
    var layer = layui.layer;
    

    form.on('submit(add-submit)', function(obj){
      //转化格式
      const dstObj = {
        name: obj.field.name,
        desc: obj.field.desc,
        sms: obj.field.sms,
        template: obj.field.template,
        payload: []
      };
      for (let key in obj.field) {
        if (key.startsWith('payload')) {
          dstObj.payload.push(obj.field[key]);
        }
      }
      //post提交
      $.post("{:U('Ajax/ajax_project_add')}" , JSON.stringify(dstObj) ,function(d){
        if(d.code == 0){
          layer.msg(d.msg, {
            icon: 1,
            time: 1000
            }, function(){
              window.parent.refreshTable();
              window.parent.layer.closeAll();
          });
        }else{
          layer.msg(d.msg);
        }
      },"json");
    });


    // checkbox和radio鼠标悬停事件
    $('.layui-form-radio, .layui-form-checkbox').hover(function() {
      layer.tips($(this).prev().data('tips'), this, {tips: [1, '#78BA32'], time: 0});
    }, function() {
        layer.closeAll('tips'); // 鼠标离开时关闭提示框
    });
  });
  </script>
</body>
</html>